package com.shundr.shipper.cargo.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.shundr.shipper.R;
import com.shundr.shipper.cargo.model.CargoTemplateInfo;
import com.shundr.shipper.common.util.DateUtil;
import com.shundr.shipper.frame.util.StringUtil;

/**
 * 
 * CargoTemplateAdapter 订单列表适配器
 * 
 * @author larry 2015-4-13 下午4:26:47 
 * @version 1.0.0
 *
 */
public class CargoTemplateAdapter extends BaseAdapter{
	public List<CargoTemplateInfo> mList;
	private Context mContext;
	
	private OnTemSelectListener selectListener;
	
	private boolean mFlag = false;

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int index) {
		return mList.get(index);
	}

	@Override
	public long getItemId(int id) {
		return id;
	}

	public CargoTemplateAdapter(List<CargoTemplateInfo> mlist, Context mContext,boolean flag) {
		super();
		this.mList = mlist;
		this.mContext = mContext;
		this.mFlag = flag;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup viewGroup) {
		ViewHold viewHold = null;
		if(convertView == null){
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_template_cargo,null);
			viewHold = new ViewHold();
			viewHold.tvFrom = (TextView) convertView.findViewById(R.id.tv_from);
			viewHold.tvTo = (TextView) convertView.findViewById(R.id.tv_to);
			viewHold.tvType = (TextView) convertView.findViewById(R.id.tv_cargo_type);
			viewHold.tvWeight = (TextView) convertView.findViewById(R.id.tv_cargo_weight);
			viewHold.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
			viewHold.btnStatus = (Button) convertView.findViewById(R.id.btn_status);
			convertView.setTag(viewHold);
		}else{
			viewHold = (ViewHold) convertView.getTag();
		}
		try {
			CargoTemplateInfo info = mList.get(position);
			if (StringUtil.isNull(info.getCargoSrcPlace())) {
				viewHold.tvFrom.setText("起点未知");
			} else {
				viewHold.tvFrom.setText(info.getCargoSrcPlace());
			}
			if (StringUtil.isNull(info.getCargoDstPlace())) {
				viewHold.tvTo.setText("终点未知");
			} else {
				viewHold.tvTo.setText(info.getCargoDstPlace());
			}
			viewHold.tvType.setText(info.getCargoCategory());
			viewHold.tvWeight.setText(info.getCargoAmount() + " " + info.getCargoUnitName());
			viewHold.tvTime.setText(DateUtil.getInterval(info.getCargoInsertTime()));
			
//			viewHold.tvStatus.setVisibility(View.GONE);
			viewHold.btnStatus.setText("选中");
			viewHold.btnStatus.setBackgroundResource(R.drawable.btn_orange_light_selector);
			
			viewHold.btnStatus.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					try {
						selectListener.onSelect(position);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
			
			if(mFlag){
				viewHold.btnStatus.setVisibility(View.VISIBLE);
			}else{
				viewHold.btnStatus.setVisibility(View.INVISIBLE);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	static class ViewHold{
		TextView tvFrom;
		TextView tvTo;
		TextView tvType;
		TextView tvWeight;
		TextView tvTime;
		Button btnStatus;
	}
	
	public void setSelectListener(OnTemSelectListener selectListener) {
		this.selectListener = selectListener;
	}

	public interface OnTemSelectListener{
		public void onSelect(int position);
	}
}
